
//
// 130425 - AH - Created.
//

package org.japril.swing.jutil.xmloperations;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.japril.jutil.xmloperations.AbstractXmlOperation;
import org.japril.jutil.xmloperations.XmlOperation;

/**
 * <p>
 *   Dialog to view a
 *   {@link XmlOperation}'s description.
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class XmlOperationDescDialog extends JDialog 
{
  private XmlOperation m_oper=null;
  
  private final JTextArea m_desc=new JTextArea();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public XmlOperationDescDialog(){ buildGui(); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p Dialog's parent.
   */
  public XmlOperationDescDialog(Frame p){ super(p); buildGui(); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p Dialog's parent.
   */
  public XmlOperationDescDialog(Dialog p){ super(p); buildGui(); }
  
  // Invoked from constructors.
  private void buildGui()
  {
    setModal(true);
    setTitle("Operation Description");
    
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    
    c.add(new JScrollPane(m_desc));
    
    JPanel buttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttons.add(new JButton(m_ok));
    buttons.add(new JButton(m_cancel));
    c.add(buttons, BorderLayout.SOUTH);
    
    pack();
    setSize(400, 300);
  }
  
  private final Action m_ok=new AbstractAction("OK")
  {
    public void actionPerformed(ActionEvent e)
    {
      String desc=m_desc.getText();
      ((AbstractXmlOperation)m_oper).setDesc(desc);
      dispose();
    }
  }; 
  
  private final Action m_cancel=new AbstractAction("Cancel")
  {
    public void actionPerformed(ActionEvent e){ dispose(); }
  };
  
  /**
   * <p>
   *   Shows 
   *   {@link Dialog}.
   * </p>
   * @param o {@link XmlOperation} to view description for.
   */
  public void showDialog(XmlOperation o)
  {
    if (o==null)
    {
      throw new NullPointerException("o");
    }
    
    m_oper=o;
    
    String desc=getDesc(o);
    m_desc.setText(desc);
    
    m_desc.setEditable(o instanceof AbstractXmlOperation);
    
    setVisible(true);
  }
  
  private String getDesc(XmlOperation o)
  {
    if (o instanceof AbstractXmlOperation)
    {
      String retVal=((AbstractXmlOperation)o).getDesc();
      return retVal==null ? "" : retVal;
    }
    else
    {
      return o.getClass().getName();
    }
  }
  
}
